<?PHP

if(!class_exists('OP_Widget')){
	class OP_Widget{
		var $id = '',
			$name = '',
			$description = '',
			$defaults = array(),
			$width = '',
			$height = '';
		
		function OP_Widget($id){
			$this->id = $id;
		}
		
		function setMeta($name,$description){
			$this->name = $name;
			$this->description = $description;
		}
		
		function setDefaults($defaults){
			$this->defaults = $defaults;
		}
		
		function setAdminDimensions($width,$height){
			$this->width = $width;
			$this->height = $height;
		}
	}
}

if(!class_exists('OP_WidgetTemplate')){
	class OP_WidgetTemplate{
		var $widget;
		
		function OP_WidgetTemplate($widget){
			$this->widget = $widget;
		}
		
		function init(){
			$this->widget->class = 'widget_op_'.$this->widget->id; // set widget class
			
			if ( !$options = get_option($this->widget->class) ){ // init widget options
				$options = array();
				add_option($this->widget->class,$options);
			}
		
			$widget_ops = array('classname' => $this->widget->class, 'description' => __($this->widget->description));
			
			$control_ops = array('id_base' => $this->widget->id);
			$control_ops['width'] = $this->widget->width;
			$control_ops['height'] = $this->widget->width;
			
			$name = __($this->widget->name);
			
			$id = false;
			
			foreach ( array_keys($options) as $o ) {
				$id = $this->widget->id.'-'.$o; // Never never never translate an id
				wp_register_sidebar_widget($id, $name, array(&$this,'_render'), $widget_ops, array( 'number' => $o ));
				if (is_admin()) { wp_register_widget_control($id, $name, array(&$this,'_control'), $control_ops, array( 'number' => $o )); }
			}
			
			if ( !$id ) {
				wp_register_sidebar_widget( $this->widget->id.'-1', $name, array(&$this,'_render'), $widget_ops, array( 'number' => -1 ) );
				if (is_admin()) { wp_register_widget_control( $this->widget->id.'-1', $name, array(&$this,'_control'), $control_ops, array( 'number' => -1 ) ); }
			}

			global $pagenow;
			if(in_array($pagenow,array('profile.php','wp-login.php'))){ // profile page is recognized as admin but requires different actions since it is user facing
				add_action('wp_head', array(&$this,'register_head'));
				add_action('wp_head', create_function('','wp_print_scripts();'));
			}
			else if(is_admin()){
					wp_register_script('opwjs', get_bloginfo('template_directory').'/op/admin/scripts/opw.js',array('jquery','jquery-ui-sortable'));
					wp_enqueue_script('opwjs');
					add_action('init',array(&$this,'admin_head'));
			}
			else{
				add_action('template_redirect', array(&$this,'register_head'));
			}
		}
		
		function register_head(){
			if($this->is_active()) $this->head();
		}
		
		function is_active(){
			global $OnePress;
			
			$sidebars_widgets = wp_get_sidebars_widgets(false);
			
			/**
			 * If OnePress has not been instantiated, load widget anyways
			 **/
			if($OnePress == NULL) return TRUE;
			else{
				$layouts = $OnePress->layout->layouts[$OnePress->layout->key];
				foreach($layouts as $layout){
					if ( is_array($sidebars_widgets[$OnePress->layout->key.'-'.$layout]) ) foreach ( $sidebars_widgets[$OnePress->layout->key.'-'.$layout] as $widget ){
						$tmp_widget = explode('-',$widget);
						if ($this->widget->id == $tmp_widget[0])
							return TRUE;
					}
				}
			}
			return FALSE;
		}
		
		function _render($args, $widget_args = 1){
			extract( $args, EXTR_SKIP );
			if ( is_numeric($widget_args) )
				$widget_args = array( 'number' => $widget_args );
			$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
			extract( $widget_args, EXTR_SKIP );
		
			$options = get_option($this->widget->class);
	
			$this->values = array_merge($options[$number],$args,$widget_args);
			
			// before widget
			echo str_replace('class="widget ', 'class="widget '.$this->values['style'].' ',$this->values['before_widget']);
			
			$this->render();
			
			echo $this->values['after_widget'];
		}
		
		function _control($widget_args){
			global $wp_registered_widgets;
			static $updated = false;
			
			if(is_numeric($widget_args))
				$widget_args = array( 'number' => $widget_args );
			$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
			extract( $widget_args, EXTR_SKIP );
			
			$stored_options = get_option($this->widget->class);
	
			if ( !is_array($stored_options) )
				$stored_options = array();
				
			if ( !$updated && !empty($_POST['sidebar']) ) {
				$sidebar = (string) $_POST['sidebar'];
		
				$sidebars_widgets = wp_get_sidebars_widgets();
				
				if ( isset($sidebars_widgets[$sidebar]) )
					$this_sidebar =& $sidebars_widgets[$sidebar];
				else
					$this_sidebar = array();
				
				foreach ( $this_sidebar as $_widget_id ) {
					if ( $this->widget->class.'_render' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
						$widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
						
						if ( !in_array( "{$this->widget->id}-$widget_number", $_POST['widget-id'] ) ) // the widget has been removed.
							unset($options[$widget_number]);
					}
				}
				
				foreach ( (array) $_POST[$this->widget->class] as $widget_number => $widget_values ) {
					if ( !isset($widget_values['ismulti']) && isset($stored_options[$widget_number]) ) // user clicked cancel
						continue;
						
					$new_widget_values = $this->do_post($widget_values);
					$new_widget_values['style'] = isset($new_widget_values['style']) ? $new_widget_values['style'] : $widget_values['style'];

					$stored_options[$widget_number] = $new_widget_values;
				}
				
				update_option($this->widget->class, $stored_options);
				$updated = true;
				
			}
			
			if ( -1 == $number ) { // define defaults here
				$this->values = array_merge(array('number' => '%i%', 'style' => ''), $this->widget->defaults);
			} else {
				$this->values = $stored_options[$number];
				$this->values['number'] = $number;
			}

			$this->options();
			echo '<p>',
				'<label for="'.$this->widget->class.'-style-'.$this->values['number'].'">'._e('Extra class:').'</label> <input type="text" id="'.$this->widget->class.'-style-'.$this->values['number'].'" name="'.$this->widget->class.'['.$this->values['number'].'][style]" value="'.$this->values['style'].'" />',
				' <a href="#" onclick="jQuery(this).prev().val(\'\'); return false;">default</a>',
				'</p>';
			
			echo '<input type="hidden" name="'.$this->widget->class.'['.$this->values['number'].'][ismulti]" value="1" />'; // track if multi widget
			echo '<input type="hidden" name="'.$this->widget->class.'['.$this->values['number'].'][submit]" value="1" />';
		}
		
		function admin_head(){}
		function head(){}
		function options(){}
		function render(){}
		function do_post($post){}
	}
}

?>